package cn.xiaoxiang.pet.service.impl;

import cn.xiaoxiang.basic.config.BussinessException;
import cn.xiaoxiang.basic.util.AjaxResult;
import cn.xiaoxiang.basic.util.BaiduAuditUtils;
import cn.xiaoxiang.pet.domain.Pet;
import cn.xiaoxiang.pet.domain.PetDetail;
import cn.xiaoxiang.pet.domain.PetOnlineAuditLog;
import cn.xiaoxiang.pet.mapper.PetDetailMapper;
import cn.xiaoxiang.pet.mapper.PetMapper;
import cn.xiaoxiang.pet.mapper.PetOnlineAuditLogMapper;
import cn.xiaoxiang.pet.service.IPetService;
import cn.xiaoxiang.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoxiang
 * @since 2022-12-03
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetOnlineAuditLogMapper auditLogMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper detailMapper;

    @Override
    public void add(Pet pet) {
        //状态
        pet.setState(0);
        //创建时间
        pet.setCreatetime(new Date());
        //id
//        employeeMapper.findById().getId();
//        pet.setShopId(e);
        //todo 没有写
//        pet.setShopId(40L);
        super.add(pet);
        //添加详情
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        detailMapper.add(detail);
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
        throw new BussinessException("参数不能为空");
        }
        //过略调已经商家的
        List<Pet> petList=petMapper.findByIdAndstate(ids);
        if(CollectionUtils.isEmpty(petList)){
            return AjaxResult.me().fail("商家不能为空");
        }
        //百度Ai
        List<PetOnlineAuditLog> auditLogs=new ArrayList<>();
        List<Long> petIds=new ArrayList<>() ;
        petList.forEach(pet->{
            StringBuilder sb = new StringBuilder();
            if(!BaiduAuditUtils.TextCensor(pet.getName())){

                sb.append("宠物名称不通过，名称为："+pet.getName());
            }
            //审合图片
            String resources = pet.getResources();
            Arrays.stream(resources.split(",")).forEach(img->{
             if(!BaiduAuditUtils.ImgCensor(img)){
                 sb.append("图片不通过，名称为："+pet.getName()).append(",");
             }
            });
            PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
            if(sb.length()>0){

            auditLog.setAuditId(-1L);
            auditLog.setAuditTime(new Date());
            auditLog.setNote(sb.toString());
            auditLog.setState(0);
            auditLog.setPetId(pet.getId());
//                auditLogMapper.add(auditLog);
           auditLogs.add(auditLog);
            return;
            }
//            Pet updatePet = new Pet();
//            updatePet.setId(pet.getId());
//        updatePet.setState(1);
//        petMapper.update(updatePet);
            petIds.add(pet.getId());
        });


        //通过上架
        if(!CollectionUtils.isEmpty(petIds)){
            petMapper.batchUpdate(petIds);
        }
        //未通过
        if (!CollectionUtils.isEmpty(auditLogs)){
            auditLogMapper.batchAdd(auditLogs);
            String collect = auditLogs.stream().map(PetOnlineAuditLog::getNote).collect(Collectors.joining());
            return AjaxResult.me().fail("没有东西"+collect);
        }
        return AjaxResult.me().success();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        List<Pet> petList=petMapper.findByIdAndoffsale(ids);
        if(CollectionUtils.isEmpty(petList)){
            return AjaxResult.me().fail("商家不能为空");
        }
        if(!CollectionUtils.isEmpty(ids)){
            petMapper.offsaleUpdate(ids);
        }
        return AjaxResult.me().success();
    }
}
